import type { AiModel, ConversationHistoryItem, ModelDisplayConfig, ModelDetails } from '@/types/chat'

// 时间格式化函数
export const formatHistoryTime = (timestamp: Date) => {
  const now = new Date()
  const diff = now.getTime() - timestamp.getTime()
  const minutes = Math.floor(diff / (1000 * 60))
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (minutes < 60) {
    return `${minutes}分钟前`
  } else if (hours < 24) {
    return `${hours}小时前`
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return timestamp.toLocaleDateString('zh-CN', {
      month: 'short',
      day: 'numeric'
    })
  }
}

export const formatDetailedTime = (timestamp: Date) => {
  if (!timestamp) return '未知时间'
  
  try {
    const date = typeof timestamp === 'string' ? new Date(timestamp) : timestamp
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch (error) {
    return '时间格式错误'
  }
}

export const formatTimestamp = (timestamp: Date | string | undefined) => {
  if (!timestamp) return '未知时间'
  
  try {
    const date = typeof timestamp === 'string' ? new Date(timestamp) : timestamp
    return date.toLocaleString().substring(5, 16)
  } catch (error) {
    return '时间格式错误'
  }
}

// 模型相关函数
export const getModelDisplayName = (modelKey: string, models: AiModel[] = []) => {
  if (!models || !Array.isArray(models)) {
    return modelKey
  }
  
  const selectedModel = models.find(model => model.modelKey === modelKey)
  if (selectedModel) {
    return selectedModel.name
  }
  
  return modelKey
}

export const getModelDisplayConfig = (modelName: string, models: AiModel[] = []): ModelDisplayConfig => {
  if (!models || !Array.isArray(models)) {
    return { iconClass: 'fas fa-robot', colorClass: 'bg-gray-100 text-gray-600' }
  }
  
  const model = models.find(m => m.name === modelName || m.modelKey === modelName)
  if (model) {
    return {
      iconClass: model.iconClass || 'fas fa-robot',
      colorClass: model.colorClass || 'bg-gray-100 text-gray-600'
    }
  }
  
  return { iconClass: 'fas fa-robot', colorClass: 'bg-gray-100 text-gray-600' }
}

// 历史记录分组
export const groupHistoryByTime = (history: ConversationHistoryItem[]) => {
  const today = new Date()
  const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate())
  const sevenDaysAgo = new Date(todayStart.getTime() - 7 * 24 * 60 * 60 * 1000)
  const thirtyDaysAgo = new Date(todayStart.getTime() - 30 * 24 * 60 * 60 * 1000)
  
  const todayRecords: ConversationHistoryItem[] = []
  const recentRecords: ConversationHistoryItem[] = []
  const olderRecords: ConversationHistoryItem[] = []
  
  history.forEach(item => {
    const itemDate = new Date(item.timestamp)
    
    if (itemDate >= todayStart) {
      todayRecords.push(item)
    } else if (itemDate >= sevenDaysAgo) {
      recentRecords.push(item)
    } else if (itemDate >= thirtyDaysAgo) {
      olderRecords.push(item)
    }
  })
  
  return {
    today: todayRecords,
    recent: recentRecords,
    older: olderRecords
  }
}

// 生成星级评分HTML
export const generateStarRating = (score: number, maxStars: number = 5) => {
  const fullStars = Math.floor(score)
  const hasHalfStar = score % 1 >= 0.5
  const emptyStars = maxStars - fullStars - (hasHalfStar ? 1 : 0)
  
  let starsHtml = ''
  
  // 满星
  for (let i = 0; i < fullStars; i++) {
    starsHtml += '<i class="fas fa-star text-orange-400"></i>'
  }
  
  // 半星
  if (hasHalfStar) {
    starsHtml += '<i class="fas fa-star-half-alt text-orange-400"></i>'
  }
  
  // 空星
  for (let i = 0; i < emptyStars; i++) {
    starsHtml += '<i class="far fa-star text-gray-300"></i>'
  }
  
  return starsHtml
}
